/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.designer.impl;

import java.util.List;

import org.soupframework.gwt.designerpanel.client.command.Command;
import org.soupframework.gwt.designerpanel.client.command.CommandGroup;
import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.designer.DesignerManager;
import org.soupframework.gwt.designerpanel.client.dragproxy.DragProxy;

import com.google.gwt.user.client.ui.AbsolutePanel;

public abstract class AbsolutePanelViewDesigner<T> extends ComplexPanelViewDesigner<T> {

	protected AbsolutePanelViewDesigner(DesignerManager<T> manager, ContainerDesigner<T> parent, T object) {
		super(manager, parent, object);
	}

	protected class Operations extends ComplexPanelViewDesigner<T>.Operations {
		@Override
		public void addObject(T object, int left, int top) {
			getObjectView().add(getChildObjectView(object), left, top);
			super.addObject(object, left, top);
		}
	}

	@Override
	public Operations getOperations(){
		if(this.operations != null){
			return (Operations)this.operations;
		}
		this.operations = this.new Operations();
		return (Operations)this.operations;
	}

	/**
	 * Moves the given object of the specified amount
	 */
	public void moveObject(T object, int deltaX, int deltaY, boolean blockGridAlignment){
		Command command = this.getManager().getCommandFactory().createMoveCommand(this, object, deltaX, deltaY, blockGridAlignment);
		this.getManager().doCommand(command);
	}

	/**
	 * Moves the given objects of the specified amount
	 */
	public void moveObjects(List<T> os, int deltaX, int deltaY, boolean blockGridAlignment){
		CommandGroup cg = this.getManager().getCommandFactory().createCommandGroup("CommandGroupTypeMoveMulti");
		for(T object : os){
			Command command = this.getManager().getCommandFactory().createMoveCommand(this, object, deltaX, deltaY, blockGridAlignment);
			cg.addCommand(command);
		}
		this.getManager().doCommand(cg);
	}

	/**
	 * Moves the given object of the specified amount. It differs from the moveObject method in that subsequent calls to moveObjectPartial
	 * cumulate to the same command group in history
	 */
	public void moveObjectPartial(String commandGroupTypePrefix, T object, int deltaX, int deltaY, boolean blockGridAlignment){
		Command command = this.getManager().getCommandFactory().createMoveCommand(this, object, deltaX, deltaY, blockGridAlignment);
		this.getManager().doPartialCommand("CommandGroupTypeMove-" + commandGroupTypePrefix + object.hashCode(), command);
	}

	/**
	 * Moves the given objects of the specified amount. It differs from the moveObjects method in that subsequent calls to moveObjectsPartial
	 * cumulate to the same command group in history
	 */
	public void moveObjectsPartial(String commandGroupTypePrefix, List<T> os, int deltaX, int deltaY, boolean blockGridAlignment){
		CommandGroup cg = this.getManager().getCommandFactory().createCommandGroup("CommandGroupTypeMoveMulti");
		for(T object : os){
			Command command = this.getManager().getCommandFactory().createMoveCommand(this, object, deltaX, deltaY, blockGridAlignment);
			cg.addCommand(command);
		}
		this.getManager().doPartialCommand("CommandGroupTypeMoveMulti-" + commandGroupTypePrefix, cg);
	}

	/**
	 * Aligns the given object to the grid.
	 *
	 * @param object The object to align to the grid
	 */
	protected void alignToGridPartial(T object){
		Command command = this.getManager().getCommandFactory().createAlignToGridCommand(this, object);
		this.getManager().doPartialCommand("AlignToGridCommandGroup-" + this.hashCode(), command);
	}

	/**
	 * Sets the grid mode
	 */
	/*@Override
	public void setGridMode(GridMode gridMode){
		super.setGridMode(gridMode);
		if(gridMode == GridMode.AUTO_ALIGN){
			for(Widget w : getInteractiveWidgets()){
				this.alignToGridPartial(w);
			}
		}
	}*/

	@Override
	protected void showDragProxyInt(DragProxy<T> dragProxy, int left, int top){
		if(this.getObjectView().getWidgetIndex(dragProxy.getProxyWidget()) >= 0){
			getObjectView().setWidgetPosition(dragProxy.getProxyWidget(), left, top);
		}
		else{
			getObjectView().add(dragProxy.getProxyWidget(), left, top);
		}
	}

	@Override
	protected void hideDragProxyInt(DragProxy<T> dragProxy){
		getObjectView().remove(dragProxy.getProxyWidget());
	}

	@Override
	public abstract AbsolutePanel getObjectView();
}
